package servlet;

import service.Impl.UserServiceImpl;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: DELL
 * @Since: 2024/10/8 19:26
 * @Description:
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out =resp.getWriter();
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        UserService userService=new UserServiceImpl();
        String opr=req.getParameter("opr");
        String path=req.getContextPath();


        if (opr.equals("register")){
            String yonghuming=req.getParameter("user");
            String  mima=req.getParameter("pwd");
            String name=req.getParameter("name");
            int gender= Integer.parseInt(req.getParameter("gender"));
            String idCardNo=req.getParameter("idCardNo");
            String email=req.getParameter("email");
            String phone=req.getParameter("phone");
            int hang=userService.register(yonghuming,name,mima,gender,idCardNo,email,phone);
            if (hang>0){
                out.print("<script>alert('注册成功!');location.href='"+path+"/pages/index.jsp'</script>");
            }else{
                out.print("<script>alert('注册失败!');location.href='"+path+"/pages/enroll.jsp'</script>");

            }
        }
        }
    }

